import datetime
from typing import List

from csr_utils import log

from commons import system_constatnts
from dao import testcase_dao, project_dao, suite_dao
from orm_domain.testcase import Testcase
from utils.bean_utils import BeanUtils

from utils.git_utils import GitUtils


class TestcaseService:

    def save(self, tc: Testcase):
        """
        测试用例保存
        @param tc:用例名称不能为空
        @return:
        """
        if BeanUtils.str_is_blank(tc.name):
            log.info("测试用例名称不能为空")
            return None
        tc.name = tc.name.strip()
        return testcase_dao.save(tc)

    def update(self, tc: Testcase):
        """
        测试用例修改
        @param tc:用例名称不能为空、
        @return:
        """
        query_testcase = testcase_dao.get_by_id(tc.id)
        if BeanUtils.str_is_blank(tc.name):
            log.info("测试用例名称不能为空")
            return None
        if query_testcase == None:
            log.info("未查到对应testcase")
            return None
        query_testcase.name = tc.name.strip()
        query_testcase.data = tc.data
        testcase_dao.update()

    def delete(self, Testcase_id):
        """
        测试用例删除
        @param Testcase_id:
        @return:
        """
        tc = testcase_dao.get_by_id(Testcase_id)
        if tc == None:
            log.info("未查到对应testcase")
            return None
        testcase_dao.delete(tc)

    def list(self) -> [Testcase]:
        """
        获取所有测试用例
        @return:测试用例列表
        """
        return testcase_dao.list()

    def list_by_project_id(self, project_id) -> [Testcase]:
        """
        根据项目id获取测试用例列表
        @param project_id:
        @return:
        """
        project = project_dao.get(id=project_id,is_del = system_constatnts.NOT_DEL_FLAG)
        if project is None:
            return None
        return project.testcases

    def list_by_suite_id(self, suite_id) -> [Testcase]:
        """
        根据测试套件id获取测试用例列表
        @param suite_id:
        @return:
        """
        suite = suite_dao.get_by_suite_id(suite_id)
        if suite is None:
            return None
        return suite.testcases

    def search_by_project_id_and_name(self, project_id, testcase_name:str) -> [Testcase]:
        """
        根据项目ip、测试用例名称搜索测试用例
        @param testcase_name:
        @return:
        """
        project = project_dao.get(id=project_id,is_del = system_constatnts.NOT_DEL_FLAG)
        if project is None:
            return None
        return [testcase for testcase in project.testcases if testcase.name.__contains__(testcase_name)]

    def get_by_id(self, id) -> Testcase:
        return testcase_dao.get_by_id(id)

    def bind_testcases_to_project(self, testcases: [Testcase], project_id):
        '''
        param: testcases:用例列表
        param: project_id:项目id
        '''

        try:
            project = project_dao.get(id=project_id,is_del = system_constatnts.NOT_DEL_FLAG)
            if project is None:
                log.info("项目id不存在")
                return None
            project.testcases = testcases
            testcase_dao.update()
        except Exception as e:
            log.info("请检查测试用例列表数据是否正常", e)
            # todo: 异常抛出
            # todo：定义一个公共的异常处理

    def unbind_testcases_to_project(self, project_id):
        """
        通过project解绑测试用例
        @param project_id:
        @return:
        """
        project = project_dao.get(id=project_id,is_del = system_constatnts.NOT_DEL_FLAG)
        if project is None:
            log.info("项目id不存在")
            return None
        project.testcases = []
        testcase_dao.update()

    def get_code_from_git(self, project_id):
        """
        # todo： 注释
        # todo: 异常捕获
        # todo: idea 配置对应信息,注释模板 -> 李旭
        """
        from service import project_service, test_git_info_service
        project = project_service.get_by_id(project_id)
        workspace = project_service.allocate_workspace(project.sign)
        # commit_id = project.version
        git_info = test_git_info_service.get_by_id(project.git_id)
        gu = GitUtils(local_path=workspace, repo_url=git_info.git_url, branch=git_info.git_branch,
                      username=git_info.git_user_name, password=git_info.git_pwd)
        if not gu.is_last:
            gu.pull()
        # todo: 返回值
        return workspace

    def get_code_from_zipfile(self, zip_file):
        """
        从压缩文件解压
        1、支持zip、rar、7z格式
        """
        pass
